package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.impl.RegionServiceImpl;
import com.jzo2o.foundations.service.impl.ServeServiceImpl;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SpringCacheSyncHandler {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RegionServiceImpl regionService;
    @Autowired
    private ServeServiceImpl serveService;
    /**
     * 更新区域缓存
     */
    @XxlJob("activeRegionCacheSync")
    public void activeRegionCacheSync()
    {
        log.info("开始区域缓存");
        //1.删除区域缓存,这里通过redisTemplate.delete()删除缓存
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGIONS");

        //2.获取区域列表 可以通过调用区域的查询方法进行缓存
        List<RegionSimpleResDTO> list = regionService.queryActiveRegionListCache();

        log.info("开始首页服务列表缓存");
        //1.删除首页服务列表缓存,首页服务需要根据区域id进行删除
        for (RegionSimpleResDTO dto : list) {
            //dto.getId();//获取到区域id,将这个区域id作为key进行删除
            //1.根据区域id删除缓存
            redisTemplate.delete(RedisConstants.CacheName.SERVE_ICON+"::"+dto.getId());
            //2.重写查询服务列表缓存
            serveService.firstPageServeList(dto.getId());
        }

        log.info("开始精品推荐缓存更新");
        //1.删除精品推荐缓存
        for (RegionSimpleResDTO dto : list) {
            //1.根据区域id删除精品推荐的缓存
            redisTemplate.delete(RedisConstants.CacheName.HOT_SERVE+"::"+dto.getId());
            //2.重写查询精品推荐缓存
            serveService.hotServeList(dto.getId());
        }
    }

}
